package com.company.app.events;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.company.app.constants.CommandParamType;
import com.company.app.entities.*;
import com.company.app.services.*;
import com.company.app.utils.*;

public class OpenPictureListenerHelper {
  public FrameValueObject openPicture(FrameValueObject fvo) {
    FileChooserHandler fileHandler = new FileChooserHandler();
    OpenPhotoHandler photohHandler = new OpenPhotoHandler();

    if (fvo.getImageValObj() != null) {
      return argumentMode(fvo, fileHandler, photohHandler);
    }
    return routineMode(fvo, fileHandler, photohHandler);
  }

  public FrameValueObject argumentMode(FrameValueObject fvo, FileChooserHandler fileHandler,
      OpenPhotoHandler photohHandler) {
    ImageValueObject imgObj = new ImageValueObject();

    if (fvo.getImageValObj().getCurrentPathsArray() == null) {
      imgObj = fileHandler.getImgByFilePath(fvo.getImageValObj().getCurrentPath());
    } else {
      imgObj = fvo.getImageValObj();
    }

    return openExecute(fvo, imgObj, photohHandler);
  }

  public FrameValueObject routineMode(FrameValueObject obj, FileChooserHandler fileHandler,
      OpenPhotoHandler photohHandler) {
    ImageValueObject imgObj = fileHandler.getImgsDataModel();
    return openExecute(obj, imgObj, photohHandler);
  }

  public FrameValueObject openExecute(FrameValueObject fvo, ImageValueObject imgObj, OpenPhotoHandler photohHandler) {
    if (imgObj != null) {
      String imagePath = imgObj.getCurrentPath();
      if (imagePath != null) {
        fvo.setImageValObj(imgObj);
        photohHandler.openPhoto(fvo, imagePath);
      }
      return fvo;
    } else {
      System.out.println(this + " openPicture:image not opened");
      return null;
    }
  }

  public FrameValueObject openImageByUrlStr(String fileUrlArgument, FrameValueObject frameData) {
    if ((fileUrlArgument.isBlank()) == false) {// 若不为空白
      ImageValueObject imageVal = settingImageValue(fileUrlArgument);
      frameData.setImageValObj(imageVal);
      openPicture(frameData);
      return frameData;
    }
    return null;
  }

  public FrameValueObject openImagesByUrl(String fileUrlArgument, FrameValueObject frameData) {
    CommonKits kits = new CommonKits();
    PathCheckHandler pathChecker = new PathCheckHandler();
    Optional<Map<CommandParamType, List<File>>> optionalMap = pathChecker.checker(fileUrlArgument);
    Map<CommandParamType, List<File>> checkResult = optionalMap.orElse(new HashMap<>());

    if ((fileUrlArgument.isBlank()) == true) {// 若为空白
      return null;
    }

    if (checkResult == null) {
      return null;
    }

    List<File> folderLst = checkResult.get(CommandParamType.FolderPathParam);
    List<File> fileLst = checkResult.get(CommandParamType.FilePathParam);
    ImageValueObject imageVal = new ImageValueObject();

    if (folderLst != null) {// 如果命令行参数为文件夹路径
      String[] filePathArr = kits.convertFileList2StrArr(folderLst);

      imageVal = settingImageValue2(filePathArr);
      System.out.println("openImagesByUrl(FolderPathParam):" + imageVal);

      frameData.setImageValObj(imageVal);
      openPicture(frameData);
      return frameData;
    }

    if (fileLst != null) {// 如果命令行参数为文件路径
      imageVal = settingImageValue(fileUrlArgument);
      frameData.setImageValObj(imageVal);
      openPicture(frameData);
      return frameData;
    }

    return null;
  }

  public ImageValueObject settingImageValue(String fileUrlStr) {
    ImageValueObject imageVal = new ImageValueObject();
    imageVal.setCurrentPath(fileUrlStr);
    imageVal.setCurrentOrder(0);
    return imageVal;
  }

  public ImageValueObject settingImageValue2(String[] filePathArr) {
    ImageValueObject imageVal = new ImageValueObject();
    imageVal.setCurrentPathsArray(filePathArr);
    imageVal.setCurrentPath(filePathArr[0]);
    imageVal.setCurrentOrder(0);
    return imageVal;
  }
}